/*
Copyright (c) 2009, Luke Berndt
All rights reserved.
 
 * More Information: http://www.tagflo.com

Redistribution and use in source and binary forms, with or without modification, are permitted 
provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions 
  and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
  and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of RoboTastic Software nor the names of its contributors may be used to endorse or 
  promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/


using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlServerCe;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;

namespace TagFlo
{
    public class FilterTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                int numValue = (int)value;
                return Enum.GetName(typeof(FilterType), numValue);

            }
            else
            {
                return "Error";
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }


    }

    public class ComparisonTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                int numValue = (int)value;
                return Enum.GetName(typeof(ComparisonType), numValue);

            }
            else
            {
                return "None";
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }


    }


    /// <summary>
    /// Interaction logic for SmartSetDialog.xaml
    /// </summary>
    /// 

    public class FilterDescription
    {
        private string name;
        public Collection<ComparisonDecription> comparisonList = new Collection<ComparisonDecription>();

        public Collection<ComparisonDecription> ComparisonList
        {
            get
            {
                return comparisonList;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public FilterDescription(string n, params string[] comp)
        {
            name = n;
            ComparisonDecription c;
            foreach (string s in comp)
            {
                c = new ComparisonDecription(s);
                comparisonList.Add(c);
            }
        }
    }

    public class ComparisonDecription
    {
        private string name;


        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public ComparisonDecription(string n)
        {
            name = n;
        }
    }


    public partial class SmartSetDialog : System.Windows.Window
    {
        PhotoDBEntities photoEntities;// = new PhotoDBEntities();
        SmartSet currentSmartSet = null;
        ObservableCollection<SmartSet> smartSetCollection;
        ObservableCollection<SmartSetFilter> filterCollection = new ObservableCollection<SmartSetFilter>();
        Collection<FilterDescription> filterList = new Collection<FilterDescription>();

        public void BuildComparisonComboBox()
        {



            ComboBoxItem c;


            foreach (string s in Enum.GetNames(typeof(ComparisonType)))
            {
                c = new ComboBoxItem();
                c.Content = s;
                ComparisonComboBox.Items.Add(c);

            }

        }

        public void BuildFilterList()
        {
            FilterDescription f;
            f = new FilterDescription("Tag", "is", "contains", "doesn't contain", "starts with", "ends with", "are empty", "aren't empty");
            filterList.Add(f);
            f = new FilterDescription("Title","is", "contains", "doesn't contain", "starts with", "ends with", "is empty", "is not empty");
            filterList.Add(f);
            f = new FilterDescription("Subject", "is", "contains", "doesn't contain", "starts with", "ends with", "is empty", "is not empty");
            filterList.Add(f);
            f = new FilterDescription("Comment", "is", "contains", "doesn't contain", "starts with", "ends with", "is empty", "is not empty");
            filterList.Add(f);
            f = new FilterDescription("Photo Set", "is", "is not", "starts with", "ends with");
            filterList.Add(f);
            f = new FilterDescription("Rating", "is", "is not", "is greater than", "is less than", "is greater than or equal to", "is less than or equal to");
            filterList.Add(f);
            f = new FilterDescription("ISO Speed", "is", "is not", "is greater than", "is less than", "is greater than or equal to", "is less than or equal to");
            filterList.Add(f);
            f = new FilterDescription("Exposure Time", "is", "is not", "is greater than", "is less than", "is greater than or equal to", "is less than or equal to");
            filterList.Add(f);
            f = new FilterDescription("Aperture", "is", "is not", "is greater than", "is less than", "is greater than or equal to", "is less than or equal to");
            filterList.Add(f);
            f = new FilterDescription("Focal Length", "is", "is not", "is greater than", "is less than", "is greater than or equal to", "is less than or equal to");
            filterList.Add(f);
            f = new FilterDescription("Camera Model", "is", "contains", "doesn't contain", "starts with", "ends with", "is empty", "is not empty");
            filterList.Add(f);
            
        }

        public SmartSetDialog(PhotoDBEntities entities)
        {
            photoEntities = entities;
            InitializeComponent();
            smartSetCollection = new ObservableCollection<SmartSet>(photoEntities.SmartSets.Include("SmartSetFilters"));
            
            BuildFilterList();
            SmartSetListBox.DataContext = smartSetCollection;
            RulesListView.DataContext = filterCollection;
            FilterTypeComboBox.DataContext = filterList;
        }


        private void ChangeSmartSet(SmartSet smartSet)
        {
            currentSmartSet = smartSet;
            filterCollection = new ObservableCollection<SmartSetFilter>(currentSmartSet.SmartSetFilters);
            RulesListView.DataContext = filterCollection;

        }
        private void SmartSetListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SmartSet selectedSmartSet = (SmartSet)SmartSetListBox.SelectedItem;
            if (selectedSmartSet != null)
            {
                ChangeSmartSet(selectedSmartSet);   
               
            }
        }

        private bool SmartSetExists(string name)
        {
            return smartSetCollection.Any(s => s.SmartSetName == name);

        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            if (SmartSetExists(SmartSetNameTextBox.Text))
            {
                return;
            }

          SmartSet smartSet = SmartSet.New();
          smartSet.SmartSetName = SmartSetNameTextBox.Text;
photoEntities.AddToSmartSets(smartSet);
            smartSetCollection.Add(smartSet);
            photoEntities.SaveChanges();
            ChangeSmartSet(smartSet);
        }

        private void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            RulesListView.DataContext = null;
            foreach (SmartSetFilter filter in currentSmartSet.SmartSetFilters.ToList())
            {
                photoEntities.DeleteObject(filter);
            }
            photoEntities.DeleteObject(currentSmartSet);
            smartSetCollection.Remove(currentSmartSet);
            currentSmartSet = null;
            photoEntities.SaveChanges();
            filterCollection = null;
        }


        private void AddRuleButton_Click(object sender, RoutedEventArgs e)
        {

            if (currentSmartSet == null)
            {
                //create a diloag here, asking user to select a set
                return;
            }

           SmartSetFilter filter = SmartSetFilter.New();
              filter.FilterType = FilterTypeComboBox.Text;
                filter.ComparisonType = ComparisonComboBox.Text;
                filter.FilterValue = FilterValueComboBox.Text;
          
            currentSmartSet.SmartSetFilters.Add(filter);
            filterCollection.Add(filter);
            photoEntities.SaveChanges();           
          
        }
        private void RemoveRuleButton_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;  // sender is the button user clicked
            SmartSetFilter filter = (SmartSetFilter)b.DataContext;

            currentSmartSet.SmartSetFilters.Remove(filter);
            
            filterCollection.Remove(filter);
            photoEntities.DeleteObject(filter);
            photoEntities.SaveChanges();
           
        }
        private void FilterTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FilterDescription d = (FilterDescription)FilterTypeComboBox.SelectedItem;
        }

    }
}